import 'package:flutter/material.dart';
import 'package:flutter_app_getx/page/controller/login_controller.dart';
import 'package:get/get.dart';

class LoginView extends GetView<LoginController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: PreferredSize(
        preferredSize: Size.fromHeight(0),
        child: AppBar(elevation: 0, backgroundColor: Colors.red),
      ),
      body: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            inputWidget(
              label: "用户名",
              hintText: "用户名",
              cb: (val) => controller.saveUserName(val),
            ),
            inputWidget(
              label: "密码",
              hintText: "密码",
              obscureText: true,
              cb: (val) => controller.savePwd(val),
            ),
            loginButton()
          ],
        ),
      ),
    );
  }

  // 输入框
  Widget inputWidget(
      {String label, String hintText, Function cb, bool obscureText = false}) {
    return TextField(
      obscureText: obscureText,
      decoration: InputDecoration(
        labelText: label + '：',
        icon: Icon(Icons.people),
        hintText: "请输入" + hintText,
        hintStyle: TextStyle(color: Colors.green),
      ),
      onChanged: (val) => cb(val),
    );
  }

  // 登录
  Widget loginButton() => Container(
    width: double.infinity,
    height: 45,
    margin: const EdgeInsets.only(top: 20),
    color: Colors.blue,
    child: FlatButton(
      onPressed: () {
        // 延迟两秒跳转 传参的
        Future.delayed(Duration(seconds: 2), () {
          Get.toNamed(
              "/tab?name=${controller.userName}&pwd=${controller.pwd}");
        });
      },
      child: Text(
        "登录",
        style: TextStyle(color: Colors.white),
      ),
    ),
  );
}
